﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Phone.Tasks;
using LocalSettings;

namespace QuickTasks
{

    public class FavoritePeople
    {
        # region Global

        IsolatedStorageFile isostore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream isostream = null;
        XDocument xdoc = null;
        IsolatedStorageFile isostore1 = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream isostream1 = null;
        XDocument xdoc1 = null;

        #endregion

        #region Events

        public void SavePeople(string name, string mobile, string home, string work, string emailId ,string favid)
        {

            if (favid != "")
            {
                try
                {
                    if (isostore.FileExists("QuickTasks/People.xml"))
                    {
                        isostream = new IsolatedStorageFileStream("QuickTasks/People.xml", FileMode.Open, isostore);
                        xdoc = XDocument.Load(isostream);
                        isostream.Close();

                        var addressid = from i in xdoc.Descendants("people") where i.Attribute("id").Value == favid select i;

                        foreach (var rep in addressid)
                        {
                            rep.Element("name").Value = name;
                            rep.Element("mobile").Value = mobile;
                            rep.Element("home").Value = home;
                            rep.Element("work").Value = work;
                            rep.Element("DefaultPlace").Value = IsolatedSettings.getSettingsStringValue("DefaultPlace");
                            rep.Element("emailId").Value = emailId;

                        }
                        isostream = new IsolatedStorageFileStream("QuickTasks/People.xml", FileMode.Create, isostore);
                        xdoc.Save(isostream);
                        isostream.Close();
                    }
                }
                catch
                {
                }
            }
            else
            {
                try
                {

                    if (isostore.FileExists("QuickTasks/People.xml"))
                    {
                        isostream = new IsolatedStorageFileStream("QuickTasks/People.xml", FileMode.Open, isostore);
                        xdoc = XDocument.Load(isostream);
                        isostream.Close();
                        int id = 1;
                        var favoriteid = from i in xdoc.Descendants("people") orderby i.Attribute("id").Value descending select i;
                        foreach (XElement ele in favoriteid)
                        {
                            id = Convert.ToInt32(ele.Attribute("id").Value);
                            id++;
                            break;
                        }
                        xdoc.Root.Add(new XElement("people",
                            new XAttribute("id", id),
                            new XElement("name", name),
                            new XElement("mobile", mobile),
                            new XElement("home", home),
                            new XElement("work", work),
                           new XElement("DefaultPlace", IsolatedSettings.getSettingsStringValue("DefaultPlace")),
                            new XElement("emailId", emailId)));
                        isostream = new IsolatedStorageFileStream("QuickTasks/People.xml", FileMode.Create, isostore);
                        xdoc.Save(isostream);
                        isostream.Close();
                    }
                    else
                    {
                        isostream = new IsolatedStorageFileStream("QuickTasks/People.xml", FileMode.Create, isostore);
                        xdoc = new XDocument(new XElement("Favorite",
                            new XElement("people",
                                new XAttribute("id", "1"),
                                new XElement("name", name),
                                new XElement("mobile", mobile),
                                new XElement("home", home),
                                new XElement("work", work),
                                new XElement("DefaultPlace", IsolatedSettings.getSettingsStringValue("DefaultPlace")),
                                     new XElement("emailId", emailId))));
                        xdoc.Save(isostream);
                        isostream.Close();
                    }
                }
                catch (Exception ex)
                {
                    Exception exnew = new Exception("Exception in SavePeople Method In FavoritePeople file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                    throw exnew;
                }
            }
        }

        public List<FavoritePeopleProp> LoadPeopleDetails()
        {
            List<FavoritePeopleProp> objfavorite = new List<FavoritePeopleProp>();
            try
            {
                if (isostore.FileExists("QuickTasks/People.xml"))
                {
                    isostream = new IsolatedStorageFileStream("QuickTasks/People.xml", FileMode.Open, isostore);
                    xdoc = XDocument.Load(isostream);
                    isostream.Close();
                    //xdoc = XDocument.Load("Sample/People.xml");
                    var xqueryfavlist = from i in xdoc.Descendants("people") select i;
                    foreach (var objlist in xqueryfavlist)
                    {
                        FavoritePeopleProp objfav = new FavoritePeopleProp();
                        if (objlist.Attribute("id") != null) objfav.Id = objlist.Attribute("id").Value;
                        if (objlist.Element("name") != null) objfav.Name = objlist.Element("name").Value;
                        if (objlist.Element("mobile") != null) objfav.Mobile = objlist.Element("mobile").Value;
                        if (objlist.Element("home").Value != "")
                        {
                            objfav.Home = objlist.Element("home").Value;
                            objfav.HomeVisibility = Visibility.Visible;
                        }
                        else
                            objfav.HomeVisibility = Visibility.Collapsed;
                        if (objlist.Element("work").Value != "")
                        {
                            objfav.Work = objlist.Element("work").Value;
                            objfav.WorkVisibility = Visibility.Visible;
                        }
                        else
                            objfav.WorkVisibility = Visibility.Collapsed;
                        if (objlist.Element("emailId") != null) objfav.EmailId = objlist.Element("emailId").Value;
                        objfavorite.Add(objfav);
                    }
                }

            }
            catch (Exception ex)
            {
                Exception exnew = new Exception("Exception in LoadPeopleDetails Method In FavoritePeople file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                throw exnew;
            }

            return objfavorite;
        }

        public void GetPhoneNumbers(string id, string type)
        {
            string phoneNumber = "";
            string name = "";
            try
            {
                if (isostore.FileExists("QuickTasks/People.xml"))
                {
                    isostream = new IsolatedStorageFileStream("QuickTasks/People.xml", FileMode.Open, isostore);
                    xdoc = XDocument.Load(isostream);
                    isostream.Close();
                    // xdoc = XDocument.Load("Sample/People.xml");
                    var xqueryfavlist = from i in xdoc.Descendants("people") where i.Attribute("id").Value == id select i;
                    foreach (var objlist in xqueryfavlist)
                    {
                        phoneNumber = objlist.Element(type).Value;
                        name = objlist.Element("name").Value;
                        break;
                    }
                    PhoneCallTask phoneCallTask = new PhoneCallTask();
                    phoneCallTask.PhoneNumber = phoneNumber;
                    phoneCallTask.DisplayName =name;
                    phoneCallTask.Show();
                }
            }
            catch (Exception ex)
            {
                Exception exnew = new Exception("Exception in GetPhoneNumbers Method In FavoritePeople file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                throw exnew;
            }
        }

        public List<FavoritePeopleProp> LoadFavorites()
        {
            List<FavoritePeopleProp> objFavList = new List<FavoritePeopleProp>();
            try
            {
                if (isostore.FileExists("QuickTasks/Contacts.xml"))
                {
                    isostream = new IsolatedStorageFileStream("QuickTasks/Contacts.xml", FileMode.Open, isostore);
                    xdoc = XDocument.Load(isostream);
                    isostream.Close();

                    var FavEmails = from i in xdoc.Descendants("contact") select i;
                    foreach (var email in FavEmails)
                    {
                        FavoritePeopleProp objFav = new FavoritePeopleProp();
                        objFav.Name = email.Element("name").Value;
                        objFav.Mobile = email.Element("mno").Value;
                        objFav.pemail = email.Element("pemail").Value;
                        objFav.wemail = email.Element("wemail").Value;
                        objFav.oemail = email.Element("oemail").Value;



                        if (objFav.pemail == "")
                        {
                            objFav.pvis = Visibility.Collapsed;
                        }
                        else
                        {
                            objFav.ptype = "Personal";
                            objFav.pvis = Visibility.Visible;
                        }
                        if (objFav.wemail == "")
                        {
                            objFav.wvis = Visibility.Collapsed;
                        }
                        else
                        {
                            objFav.wtype = "Work";
                            objFav.wvis = Visibility.Visible;
                        }
                        if (objFav.oemail == "")
                            objFav.ovis = Visibility.Collapsed;
                        else
                        {
                            objFav.otype = "Other";
                            objFav.ovis = Visibility.Visible;
                        }
                        objFavList.Add(objFav);
                    }

                }
            }
            catch (Exception ex)
            {
                Exception exnew = new Exception("Exception in LoadFavoriteEmail Method In FavoritePeople file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                throw exnew;
            }
            return objFavList;
        }

        public List<FavoritePeopleProp> LoadFavotitePeopleCities(string id)
        {
            List<FavoritePeopleProp> objFavList = new List<FavoritePeopleProp>();
            if (isostore.FileExists("QuickTasks/People.xml"))
            {
                isostream = new IsolatedStorageFileStream("QuickTasks/People.xml", FileMode.Open, isostore);
                xdoc = XDocument.Load(isostream);
                isostream.Close();
             
                var FavEmails = from i in xdoc.Descendants("people") where i.Element("DefaultPlace").Value == id select i;
                foreach (var objlist in FavEmails)
                {
                    
                   
                    FavoritePeopleProp objfav = new FavoritePeopleProp();
                    if (objlist.Attribute("id") != null) objfav.Id = objlist.Attribute("id").Value;
                    if (objlist.Element("name") != null) objfav.Name = objlist.Element("name").Value;
                    
                    if (objlist.Element("DefaultPlace") != null) objfav.DefaultPlace = objlist.Element("DefaultPlace").Value;
                    if (objlist.Element("mobile").Value != "")
                    {
                        objfav.Mobile = objlist.Element("mobile").Value;
                        objfav.mobileVisibility = Visibility.Visible;
                    }
                    else
                        objfav.mobileVisibility = Visibility.Collapsed;
                    if (objlist.Element("home").Value != "")
                    {
                        objfav.Home = objlist.Element("home").Value;
                        objfav.HomeVisibility = Visibility.Visible;
                    }
                    else
                        objfav.HomeVisibility = Visibility.Collapsed;
                    if (objlist.Element("work").Value != "")
                    {
                        objfav.Work = objlist.Element("work").Value;
                        objfav.WorkVisibility = Visibility.Visible;
                    }
                    else
                        objfav.WorkVisibility = Visibility.Collapsed;
                    if (objlist.Element("emailId") != null) objfav.EmailId = objlist.Element("emailId").Value;
                    objFavList.Add(objfav);
                }
            }
            return objFavList;
        }
         public List<FavoritePeopleProp> LoadFavotitePeople(string id)
        {
            List<FavoritePeopleProp> objFavList = new List<FavoritePeopleProp>();
            if (isostore.FileExists("QuickTasks/People.xml"))
            {
                isostream = new IsolatedStorageFileStream("QuickTasks/People.xml", FileMode.Open, isostore);
                xdoc = XDocument.Load(isostream);
                isostream.Close();
                //xdoc = XDocument.Load("Sample/People.xml");
                var FavEmails = from i in xdoc.Descendants("people") where i.Attribute("id").Value == id select i;
                foreach (var email in FavEmails)
                {
                    FavoritePeopleProp objFav = new FavoritePeopleProp();
                    objFav.Id = email.Attribute("id").Value;
                    objFav.Name = email.Element("name").Value;
                    objFav.DefaultPlace = email.Element("DefaultPlace").Value;
                    objFavList.Add(objFav);
                }
            }
            return objFavList;
        }
         public List<FavoritePeopleProp> LoadDefaultPeople()
         {
             string defaultvalue;
             List<FavoritePeopleProp> objfavorite = new List<FavoritePeopleProp>();
            
             try
             {
                 if (IsolatedSettings.getSettingsStringValue("DefaultPlace") != "")
                 {
                     defaultvalue = IsolatedSettings.getSettingsStringValue("DefaultPlace");
                 }
                 else
                 {
                     defaultvalue = "0";
                 }
                
                 if (isostore.FileExists("QuickTasks/People.xml"))
                 
                 { 
                     isostream = new IsolatedStorageFileStream("QuickTasks/People.xml", FileMode.Open, isostore);
                     xdoc = XDocument.Load(isostream);
                     isostream.Close(); 
                     var xqueryfavlist1 = from i in xdoc.Descendants("people") where i.Element("DefaultPlace").Value == "0" select i;
                     foreach (var objlist in xqueryfavlist1)
                     {
                         FavoritePeopleProp objfav = new FavoritePeopleProp();
                         if (objlist.Attribute("id") != null) objfav.Id = objlist.Attribute("id").Value;
                         if (objlist.Element("name") != null) objfav.Name = objlist.Element("name").Value;
                         if (objlist.Element("mobile") != null) objfav.Mobile = objlist.Element("mobile").Value;
                         if (objlist.Element("home").Value != "")
                         {
                             objfav.Home = objlist.Element("home").Value;
                             objfav.HomeVisibility = Visibility.Visible;
                         }
                         else
                             objfav.HomeVisibility = Visibility.Collapsed;
                         if (objlist.Element("work").Value != "")
                         {
                             objfav.Work = objlist.Element("work").Value;
                             objfav.WorkVisibility = Visibility.Visible;
                         }
                         else
                             objfav.WorkVisibility = Visibility.Collapsed;
                         if (objlist.Element("emailId") != null) objfav.EmailId = objlist.Element("emailId").Value;
                         objfavorite.Add(objfav);
                     }
                   
                 }

             }
             catch (Exception ex)
             {
                 Exception exnew = new Exception("Exception in LoadPeopleDetails Method In FavoritePeople file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                 throw exnew;
             }

             return objfavorite;
         }
        #endregion
    }
}


